import 'package:dailoz/size_config.dart';
import 'package:flutter/material.dart';

class ProgressWidget extends StatelessWidget {
  // 加载中是否显示
  final bool? loading;

  // 子布局
  final Widget child;

  // 进度提醒内容
  final String msg;

  // 加载中动画
  final Widget progress;

  // 背景透明度
  final double alpha;

  // 字体颜色
  final Color textColor;

  const ProgressWidget(
      {super.key,
      required this.loading,
      this.msg = "",
      this.progress = const CircularProgressIndicator(),
      this.alpha = 0.6,
      this.textColor = Colors.black,
      required this.child});

  @override
  Widget build(BuildContext context) {
    List<Widget> widgetList = [];
    widgetList.add(child);
    if (loading!) {
      Widget layoutProgress;
      if (msg == "") {
        layoutProgress = Center(
          child: progress,
        );
      } else {
        layoutProgress = Center(
          child: Container(
            width: SizeConfig.getProportionateFontSize(120),
            height: SizeConfig.getProportionateFontSize(120),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(4.0),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                progress,
                Container(
                  padding: const EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
                  child: Text(
                    msg,
                    style: TextStyle(
                      color: textColor,
                      fontSize: SizeConfig.getProportionateFontSize(16.0),
                      fontWeight: FontWeight.normal,
                      decoration: TextDecoration.none,
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      }
      // 增加一层黑色背景透明度为0.8
      widgetList.add(
        Opacity(
          opacity: alpha,
          child: const ModalBarrier(
            color: Colors.black87,
          ),
        ),
      );
      // 进度条
      widgetList.add(layoutProgress);
    }
    return Stack(
      children: widgetList,
    );
  }
}
